<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="required" lay-reqtext="类型不能为空">
                    <option value="">请选择</option>
                    <option value="1">学院</option>
                    <option value="2">系</option>
                    <option value="3">专业</option>
                    <option value="4">班级</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上级栏目</label>
            <div class="layui-input-block">
                <input type="text" name="parentName" id="parentName" autocomplete="off"  placeholder="上级栏目为空则为根目录"  readonly  class="layui-input"/>
                <input type="hidden" name="parentId" id="parentId"/>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认新增</button>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['jquery','form','axios','layer','selectOrg'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            selectOrg = layui.selectOrg,
            axios = layui.axios;
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var DISABLED = 'layui-btn-disabled';
            $(':button').addClass(DISABLED);
            $(':button').attr('disabled', 'disabled');
            axios({
                method:"post"
                ,url:'/org/add'
                ,data:data.field
            }).then(res=>{
                if(res.code==0){
                  layer.msg(res.message,{
                      icon:6,
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  },function () {
                      layer.close(parentIndex);
                      $(':button').removeClass(DISABLED);
                      $(':button').removeAttr('disabled');   
                      //TODO 重新加载树
                      window.reload();
                  })
                }else{
                  $(':button').removeClass(DISABLED);
                  $(':button').removeAttr('disabled');
                  layer.msg(res.message,{
                    icon:5,
                    time:2000,
                    anim:6
                  })
                }
                
            }).catch(res=>{
                $(':button').removeClass(DISABLED);
                $(':button').removeAttr('disabled');
                layer.msg("请求错误",{
                  icon:5,
                  time:2000,
                  anim:6
                })
            });
            return false;
        });
        //加载年级
        axios({
            url:"grade/list",
            method:'get'
        })
        .then(response=> {
            data = response.data.grades
            let options = '<option value="">请选择年级</option>';
            for (let obj of data) {
                options += '<option value="' + obj.id + '">' + obj.name + '</option>'
            }
            $('#gradeId').append(options);
            form.render('select');//处理动态赋值
        }).catch(errors=>{
            layui.msg("errors")
        });

        //加载上级栏目
        selectOrg.render({
            elem:'#parentName',
            selected:[],
            checked:false,
            url:'org/tree',
            done:function (data) {
                console.log(data)
                console.log(data)
                console.log(data)
                console.log(data)
                $('#parentName').val(data[0].name)
                $('#parentId').val(data[0].id)
            }
        })
    });
</script>